const LINK_MAIN = "http://ktv.fibar.cn/api/v1/";
const WXLINK = "https://api.weixin.qq.com/sns/";
const BASE_PIC_URL = "http://ktv.fibar.cn/";
const SECRET = "eKxzhDfGkvcv9MaLQdeWgSlqnX4CosiIkR17Z0oAPmNUjBOw6nlHcTfupzbFhupy";
let iconv = require("iconv.js");


function createRoom(cb) {
  wx.request({
    url: LINK_MAIN + "room/" + (new Date()).getTime(),
    method: 'get',
    header: { 'Content-Type': 'application/json' },
    dataType: 'json',
    success: function (res) {
      return typeof cb == "function" && cb(1, res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(0, {})
    }
  })
}
function getSongList(page, cb) {
  wx.request({
    url: LINK_MAIN + "songs?page=" + page,
    method: 'get',
    header: { 'Content-Type': 'application/json' },
    dataType: 'json',
    success: function (res) {
      return typeof cb == "function" && cb(1, res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(0, {})
    }
  })
}

function searchSong(word, cb) {
  return getSongList(1, cb);
}

function getBphLrc(url, cb) {
  wx.request({
    url: url,
    method: 'get',
    responseType: 'arraybuffer',
    header: {
      'Content-Type': 'application/json',
    },
    success: function (res) {

      var string = new iconv.TextDecoder("gb18030").decode(res.data);
      console.log(string);
      return typeof cb == "function" && cb(1, string);
    },
    fail: function () {
      return typeof cb == "function" && cb(0, {})
    }
  })
}
function getKasLrc(url, cb) {
  wx.request({
    url: url,
    method: 'get',
    header: {
      'Content-Type': 'application/json',
    },
    success: function (res) {
      return typeof cb == "function" && cb(1, res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(0, {})
    }
  })
}

function getKrcLrc(url, cb) {
  wx.request({
    url: url,
    method: 'get',
    header: {
      'Content-Type': 'application/json',
    },
    success: function (res) {
      return typeof cb == "function" && cb(1, res.data)
    },
    fail: function () {
      return typeof cb == "function" && cb(0, {})
    }
  })
}

function getLrc(url, cb) {
  if (url.indexOf(".txt") != -1) {
    return getKrcLrc(url, cb);
  } else if (url.indexOf(".kas") != -1) {
    return getKasLrc(url, cb);
  } else if (url.indexOf(".bph") != -1) {
    return getBphLrc(url, cb);
  }
}

function login(cb) {
  wx.login({
    success: function (res) {
      console.log(res.code)
      if (res.code) {
        //发起网络请求
        wx.request({
          url: 'http://tt.fibar.cn/api/v2/weapp/auth/',
          data: {
            code: res.code
          },
          method: 'POST',
          success: function (res) {
            cb(1, res.data)
          },
          fail: function () {
            cb(0, null)
          }
        })
      } else {
        console.log('登录失败！' + res.errMsg)
        cb(0, null)
      }
    },
    fail: function () {
      cb(0, null)
    }
  });
}

module.exports = {
  getSongList: getSongList,
  getBphLrc: getBphLrc,
  getKasLrc: getKasLrc,
  getKrcLrc: getKrcLrc,
  getLrc: getLrc,
  searchSong: searchSong,
  createRoom: createRoom,
  login: login
}  
